package com.winspread.app.viewpager

import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2
import androidx.viewpager2.widget.ViewPager2.OnPageChangeCallback
import com.google.android.material.tabs.TabLayoutMediator
import com.winspread.app.R
import com.winspread.app.databinding.ActivityViewPagerBinding
import com.winspread.mvvm.base.vm.BaseActivity
import com.winspread.mvvm.util.click

class ViewPager2Activity : BaseActivity<ViewPagerMv, ActivityViewPagerBinding>(R.layout.activity_view_pager) {


    var tabMap = mutableMapOf<String, Fragment>()

    lateinit var myAdapter: MyAdapter


    override fun initView() {
        binding.viewpager2.offscreenPageLimit = ViewPager2.OFFSCREEN_PAGE_LIMIT_DEFAULT
        count = tabMap.size
    }


    override fun initData(extras: Bundle?) {
        myAdapter = MyAdapter(tabMap, supportFragmentManager, lifecycle)
        binding.viewpager2.adapter = myAdapter
        binding.viewpager2.registerOnPageChangeCallback(object : OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                val tabCount = binding.tablayout.tabCount
                for (i in 0 until tabCount) {
                    binding.tablayout.apply {
                        getTabAt(i)?.let {
                            val view = it.customView
                           view?.apply {
                               val iv = findViewById<ImageView>(R.id.tab_iv)
                               val tv = findViewById<TextView>(R.id.tab_tv)
                               if (it.position == position) {
                                   tv.textSize = 20f
                               } else {
                                   tv.textSize = 14f
                               }
                           }


                        }

                    }
                }

            }

        })


        TabLayoutMediator(binding.tablayout, binding.viewpager2, TabLayoutMediator.TabConfigurationStrategy { tab, position ->
            tab.customView = getCustomView(position);
        }).attach()
    }


    private fun getCustomView(position:Int): View {
        val view = LayoutInflater.from(this).inflate(R.layout.tab_item, null)
        val iv = view.findViewById<ImageView>(R.id.tab_iv)
        val tv = view.findViewById<TextView>(R.id.tab_tv)
        iv.setImageDrawable(resources.getDrawable(R.drawable.tab_selector))
        tv.text = tabMap.keys.toMutableList()[position]
        tv.textSize = 14f
        return view

    }



    var count = 0

    override fun initListenter() {
        binding.butAdd.click {
            if (this::myAdapter.isInitialized) {
                val i = ++count
                myAdapter.add("mode_${i}", BlankFragment.newInstance("mode_${i}"))
            }

        }

        binding.butAdd1.click {
            if (this::myAdapter.isInitialized) {
                myAdapter.remove(tabMap.keys.toMutableList().last())
            }

        }



        binding.butRemove.click {
            if (this::myAdapter.isInitialized) {
                val take = tabMap.keys.shuffled().take(1)[0]
                myAdapter.remove(take)


            }

        }


    }


}